﻿Imports AugmentLib.Database
Imports System.Configuration.ConfigurationManager
Imports AugmentLib.InputOutput.Encryption
Imports AugmentLib.WebComponent

Partial Class contactentry
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Session("StaffId") Is Nothing Then
            Response.Redirect("Default.aspx")
        ElseIf Session.Item("StaffType").ToString = "2" Then
            Response.Redirect("Default.aspx")
        Else
            lblStaffUID.Text = Session("StaffId").ToString
        End If

        If Not Page.IsPostBack Then

            CBEBack.ConfirmText = "You are about to navigate away from this page. " & vbCrLf & "Changes will be discard!"

            If Not Request.QueryString("id") Is Nothing Then
                lblContactUID.Text = SymmetricDecryptFromHex(Request.QueryString("id"), "augment")
                LoadContact(lblContactUID.Text)
            End If

        End If
    End Sub

    Private Sub LoadContact(ByVal ContactId As String)

        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable
        Try
            objDB.OpenConnection()

            objDB.dbVariable.SQLString = "SELECT ContactId,ProspectiveType,Name,NickName,CompanyName,Department," & _
            "JobTitle,Remark,Phone,Mobile,Fax,Email,Address FROM AW_Contact WHERE StatusDelete=0 AND ContactId=@ContactId"
            objDB.AddParameter("ContactId", Data.SqlDbType.UniqueIdentifier, New Guid(ContactId))
            objDB.FillData(TempDT, "AW_Contact", Database.SQLCommandType.NormalString)
            objDB.CloseConnection()
        Catch ex As Exception

        Finally
            If (objDB IsNot Nothing) Then
                objDB.CloseConnection()
            End If
        End Try
        For Each ReadRow As Data.DataRow In TempDT.Rows
            lblContactUID.Text = ReadRow.Item("ContactId").ToString
            txtName.Text = ReadRow.Item("Name").ToString
            txtNickName.Text = ReadRow.Item("NickName").ToString
            ddlProspectiveType.SelectedValue = ReadRow.Item("ProspectiveType").ToString
            txtCompanyName.Text = ReadRow.Item("CompanyName").ToString
            txtDepartment.Text = ReadRow.Item("Department").ToString
            txtJobTitle.Text = ReadRow.Item("JobTitle").ToString
            txtRemark.Text = ReadRow.Item("Remark").ToString
            txtPhone.Text = ReadRow.Item("Phone").ToString
            txtMobile.Text = ReadRow.Item("Mobile").ToString
            txtEmail.Text = ReadRow.Item("Email").ToString
            txtFax.Text = ReadRow.Item("Fax").ToString
            txtAddress.Text = ReadRow.Item("Address").ToString
        Next

    End Sub

    Private Sub ClearAllFields()
        lblContactUID.Text = String.Empty
        txtName.Text = String.Empty
        txtNickName.Text = String.Empty
        ddlProspectiveType.SelectedIndex = 0
        txtCompanyName.Text = String.Empty
        txtDepartment.Text = String.Empty
        txtJobTitle.Text = String.Empty
        txtRemark.Text = String.Empty
        txtPhone.Text = String.Empty
        txtMobile.Text = String.Empty
        txtEmail.Text = String.Empty
        txtFax.Text = String.Empty
        txtAddress.Text = String.Empty
    End Sub

    Protected Sub BtnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSave.Click

        If lblContactUID.Text = "" Then
            'Check if username Exist
            If IsMobileExist() Then
                ltExistMobile.Text = "<script type=""text/javascript"" language=""javascript"">document.getElementById('" & btnExistMobile.ClientID & "').click();</script>"
                'Page.ClientScript.RegisterStartupScript(Me.GetType(), "btnExistMobile", "document.getElementById('" & btnExistMobile.ClientID & "').click();", True)
                Exit Sub
            End If
           
            Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
            Try
                objDB.OpenConnection()
                objDB.InitDbTrans()
                objDB.dbVariable.SQLString = "INSERT INTO AW_Contact " & _
                "(ContactId, ProspectiveType, Name, NickName, CompanyName, Department, JobTitle, " & _
                "Remark, Phone, Mobile, Fax, Email, Address, CreateDate, StatusDelete) " & _
                "VALUES (@ContactId, @ProspectiveType, @Name, @NickName, @CompanyName, @Department, @JobTitle, " & _
                "@Remark, @Phone, @Mobile, @Fax, @Email, @Address, @CreateDate, @StatusDelete)"
                lblContactUID.Text = Guid.NewGuid.ToString
                objDB.AddParameter("ContactId", Data.SqlDbType.UniqueIdentifier, New Guid(lblContactUID.Text))
                objDB.AddParameter("NickName", Data.SqlDbType.NVarChar, txtNickName.Text.Trim.ToUpper)
                objDB.AddParameter("ProspectiveType", Data.SqlDbType.Int, ddlProspectiveType.SelectedValue)
                objDB.AddParameter("Name", Data.SqlDbType.NVarChar, txtName.Text.Trim.ToUpper)
                objDB.AddParameter("CompanyName", Data.SqlDbType.NVarChar, txtCompanyName.Text.Trim.ToUpper)
                objDB.AddParameter("Department", Data.SqlDbType.NVarChar, txtDepartment.Text.Trim.ToUpper)
                objDB.AddParameter("JobTitle", Data.SqlDbType.NVarChar, txtJobTitle.Text.Trim.ToUpper)
                objDB.AddParameter("Remark", Data.SqlDbType.NVarChar, txtRemark.Text.Trim)
                objDB.AddParameter("Phone", Data.SqlDbType.NVarChar, txtPhone.Text.Trim)
                objDB.AddParameter("Mobile", Data.SqlDbType.NVarChar, txtMobile.Text.Trim)
                objDB.AddParameter("Fax", Data.SqlDbType.NVarChar, txtFax.Text.Trim)
                objDB.AddParameter("Email", Data.SqlDbType.NVarChar, txtEmail.Text.Trim)
                objDB.AddParameter("Address", Data.SqlDbType.NVarChar, txtAddress.Text.Trim.ToUpper)
                objDB.AddParameter("CreateDate", Data.SqlDbType.DateTime, Now)
                objDB.AddParameter("StatusDelete", Data.SqlDbType.TinyInt, 0)
                objDB.ExecuteQuery()
                objDB.DbTransCommit()
                objDB.CloseConnection()
                Session.Add("entry", txtName.Text.Trim & " (" & txtMobile.Text.Trim & ")")
                Response.Redirect("contact.aspx", False)
            Catch ex As Exception
                If objDB.dbVariable.objConn.State = Data.ConnectionState.Open Then
                    objDB.DbTransRollback()
                    objDB.CloseConnection()
                End If
                lblMsgError.Text = ex.Message
                divError.Visible = True
            End Try
        Else
            UpdateContact()
        End If
    End Sub

    Private Sub UpdateContact()
        'Update 
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Try
            objDB.OpenConnection()
            objDB.InitDbTrans()

            objDB.dbVariable.SQLString = "UPDATE AW_Contact SET ProspectiveType=@ProspectiveType, Name=@Name, NickName=@NickName, " & _
            "CompanyName=@CompanyName, Department=@Department, JobTitle=@JobTitle, Remark=@Remark, Phone=@Phone, " & _
            "Fax=@Fax, Email=@Email, Address=@Address, Mobile=@Mobile WHERE ContactId=@ContactId"
            objDB.AddParameter("ProspectiveType", Data.SqlDbType.TinyInt, ddlProspectiveType.SelectedValue)
            objDB.AddParameter("Name", Data.SqlDbType.NVarChar, txtName.Text.Trim.ToUpper)
            objDB.AddParameter("NickName", Data.SqlDbType.NVarChar, txtNickName.Text.Trim.ToUpper)
            objDB.AddParameter("CompanyName", Data.SqlDbType.NVarChar, txtCompanyName.Text.Trim.ToUpper)
            objDB.AddParameter("Department", Data.SqlDbType.NVarChar, txtDepartment.Text.Trim.ToUpper)
            objDB.AddParameter("JobTitle", Data.SqlDbType.NVarChar, txtJobTitle.Text.Trim.ToUpper)
            objDB.AddParameter("Remark", Data.SqlDbType.NVarChar, txtRemark.Text.Trim)
            objDB.AddParameter("Phone", Data.SqlDbType.NVarChar, txtPhone.Text.Trim)
            objDB.AddParameter("Mobile", Data.SqlDbType.NVarChar, txtMobile.Text.Trim)
            objDB.AddParameter("Fax", Data.SqlDbType.NVarChar, txtFax.Text.Trim)
            objDB.AddParameter("Email", Data.SqlDbType.NVarChar, txtEmail.Text.Trim)
            objDB.AddParameter("Address", Data.SqlDbType.NVarChar, txtAddress.Text.Trim.ToUpper)
            objDB.AddParameter("ContactId", Data.SqlDbType.UniqueIdentifier, New Guid(lblContactUID.Text))
            objDB.ExecuteQuery()
            objDB.DbTransCommit()
            objDB.CloseConnection()
            Session.Add("entryU", txtName.Text.Trim & " (" & txtMobile.Text.Trim & ")")
            Response.Redirect("contact.aspx", False)
        Catch ex As Exception
            If objDB.dbVariable.objConn.State = Data.ConnectionState.Open Then
                objDB.DbTransRollback()
                objDB.CloseConnection()
            End If
            lblMsgError.Text = ex.Message
            divError.Visible = True
            Exit Sub
        End Try
    End Sub

    Private Function IsMobileExist() As Boolean
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable
        Try
            objDB.OpenConnection()

            objDB.dbVariable.SQLString = "SELECT Mobile,ContactId FROM AW_Contact WHERE StatusDelete=0 AND Mobile=@Mobile"
            objDB.AddParameter("Mobile", Data.SqlDbType.NVarChar, txtMobile.Text.Trim)
            objDB.FillData(TempDT, "AW_Contact", Database.SQLCommandType.NormalString)
            objDB.CloseConnection()
        Catch ex As Exception

        Finally
            If (objDB IsNot Nothing) Then
                objDB.CloseConnection()
            End If
        End Try
        If TempDT.Rows.Count >= 1 Then
            lblUpContactUID.Text = TempDT.Rows(0).Item("ContactId").ToString
            Return True
        Else
            Return False
        End If

    End Function

    Protected Sub btnExistMobile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExistMobile.Click
        lblContactUID.Text = lblUpContactUID.Text
        UpdateContact()
    End Sub

    Protected Sub btnBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBack.Click
        If Session("Back") Is Nothing Then
            Response.Redirect("Contact.aspx")
        Else
            Dim strBack As String = Session("Back").ToString
            Dim arr() As String = strBack.Split("ڡ")
            Response.Redirect(arr(0).ToString)
        End If
    End Sub
End Class
